package cn.mw.cmdb.virtualization.strategy;

import cn.mw.cmdb.virtualization.dto.VirtualizationStorageDto;
import cn.mw.cmdb.virtualization.param.VirtualizationParam;
import cn.mw.cmdb.virtualization.strategy.handler.SyncEngineDataHandler;
import cn.mw.microMonitorCommon.entity.common.MwSyncEngineDto;
import cn.mw.microMonitorCommon.enums.SyncEngineTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gengjb
 * @description 同步引擎管理
 * @date 2024/6/24 10:40
 */
@Component
@Slf4j
public class SyncEngineQueryManager {

    Map<SyncEngineTypeEnum, SyncEngineDataHandler> handlerMap = new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        Map<String, SyncEngineDataHandler> beans = applicationContext.getBeansOfType(SyncEngineDataHandler.class);
        for (SyncEngineDataHandler bean : beans.values()) {
            SyncEngineTypeEnum typeEnum = bean.handlerType();
            handlerMap.put(typeEnum,bean);
        }
    }

    public List<VirtualizationStorageDto> handlerSyncEngineData(SyncEngineTypeEnum typeEnum, VirtualizationParam param, MwSyncEngineDto engineDto){
        if(typeEnum == null){
            return null;
        }
        return handlerMap.get(typeEnum).getVirtualizationSyncStorageInfo(param,engineDto);
    }

    public void syncData(SyncEngineTypeEnum typeEnum, VirtualizationParam param, MwSyncEngineDto engineDto){
        if(typeEnum == null){
            return;
        }
        handlerMap.get(typeEnum).syncVirtualizationData(engineDto,param);
    }
}
